package com.fsm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

/*******************
 *@Describe
 *@author 封神梦
 *@Time 2023/6/13
 *@Company http://com.yiview.com
 ****************/
public class StringRedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    public  void set(String key,String value) {
        //1、通过redisTemplate设置值
        redisTemplate.boundValueOps("StringKey").set("StringValue");
        redisTemplate.boundValueOps("StringKey").set("StringValue", 1, TimeUnit.MINUTES);

        //2、通过BoundValueOperations设置值
        BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
        stringKey.set("StringVaule");
        stringKey.set("StringValue", 1, TimeUnit.MINUTES);

        //3、通过ValueOperations设置值
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("StringKey", "StringVaule");
        ops.set("StringValue", "StringVaule", 1, TimeUnit.MINUTES);
    }

    public void setExpire(){
        redisTemplate.boundValueOps("StringKey").expire(1,TimeUnit.MINUTES);
        redisTemplate.expire("StringKey",1,TimeUnit.MINUTES);
    }

    //获取值
    public String getValue(){
        //1、通过redisTemplate设置值
        String str1 = (String) redisTemplate.boundValueOps("StringKey").get();

    //2、通过BoundValueOperations获取值
        BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
        String str2 = (String) stringKey.get();

    //3、通过ValueOperations获取值
        ValueOperations ops = redisTemplate.opsForValue();
        String str3 = (String) ops.get("StringKey");
        return str3;
    }

    //4)、删除key
    public  boolean deleteKey(){
          return redisTemplate.delete("StringKey");
    }
    //5)、顺序递增
    public  void order(){
        redisTemplate.boundValueOps("StringKey").increment(3L);
    }

}
